package com.sicnu.cheer.leaugebar.fragment;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.sicnu.cheer.generalmodule.adapter.base.CommonAdapter;
import com.sicnu.cheer.generalmodule.adapter.viewholder.CommonViewHolder;
import com.sicnu.cheer.generalmodule.bean.JoinLeagueBean;
import com.sicnu.cheer.generalmodule.bean.MyUser;
import com.sicnu.cheer.generalmodule.util.NetWorkUtil;
import com.sicnu.cheer.generalmodule.util.UIHelper;
import com.sicnu.cheer.im.activity.LaunchNoticeActivity;
import com.sicnu.cheer.leaugebar.R;
import com.sicnu.cheer.leaugebar.bean.ManageMenuBean;
import com.sicnu.cheer.generalmodule.fragment.base.BaseFragment;
import com.sicnu.cheer.leaugebar.contacts.activity.UserListActivity;
import com.sicnu.cheer.leaugebar.eventmodule.activity.LaunchEventActivity;
import com.sicnu.cheer.leaugebar.leaguemodle.activity.AlbumActivity;
import com.sicnu.cheer.leaugebar.managemodule.activity.RecruitMainActivity;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by cheer on 2017/1/23.
 * 管理页子布局
 */

public class ManageChildFragment extends BaseFragment {

    private ListView listView;
    private CommonAdapter mAdapter;
    private List<ManageMenuBean> list;
    private MyUser myUser;
    private ProgressDialog progressDialog;
    private boolean isManager;

    @Override
    protected int setLayoutResourceId() {
        return R.layout.fragment_manage_child;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    protected void initData(Bundle arguments) {
        super.initData(arguments);
        myUser = MyUser.getCurrentUser(mActivity, MyUser.class);
        list = new ArrayList<>();
        progressDialog=new ProgressDialog(mActivity);
        progressDialog.setMessage("加载中...");
        //init();
        //checkManager();
    }

    private void init() {
        isManager=false;
        list.clear();
        ManageMenuBean bean ;
        bean = new ManageMenuBean();
        bean.setImgResId(R.mipmap.upload_pics);
        bean.setTitle("传照片");
        bean.setDescribe("一起来丰富我们的社团墙吧");
        list.add(bean);

        bean = new ManageMenuBean();
        bean.setImgResId(R.mipmap.recruit_member);
        bean.setTitle("入社&纳新");
        bean.setDescribe("社团需要注入新鲜的血液");
        list.add(bean);
    }

    /**
     * 检查当前用户是否为管理员
     */
    public void checkManager() {
        init();
        mAdapter.notifyDataSetChanged();
        if (!NetWorkUtil.isConnect(mActivity)){
            UIHelper.ToastMessage(mActivity,"无网络，检查管理员失败");
            return;
        }
        MyUser user = MyUser.getCurrentUser(mActivity, MyUser.class);
        BmobQuery<JoinLeagueBean> query=new BmobQuery<>();
        query.addWhereEqualTo("myUser",user);
        query.addWhereEqualTo("leagueBean",user.getLeague());
        query.addWhereEqualTo("isManager",true);
        progressDialog.show();
        query.findObjects(mActivity, new FindListener<JoinLeagueBean>() {
            @Override
            public void onSuccess(List<JoinLeagueBean> result) {
                progressDialog.dismiss();
                if (!result.isEmpty()){
                    isManager=true;
                    addFunctions();
                }
            }

            @Override
            public void onError(int i, String s) {
                progressDialog.dismiss();
                UIHelper.ToastMessage(mActivity,"检查是否为管理员失败:"+s);
            }
        });

    }

    private void addFunctions() {
        ManageMenuBean bean;
        //管理员状态下才可见
        bean = new ManageMenuBean();
        bean.setImgResId(R.mipmap.create_notice);
        bean.setTitle("写通知");
        bean.setDescribe("可以一键通知社员啦");
        list.add(bean);

        bean = new ManageMenuBean();
        bean.setImgResId(R.mipmap.organize_activities);
        bean.setTitle("办活动");
        bean.setDescribe("快快行动起来，生命在于运动");
        list.add(bean);

        bean = new ManageMenuBean();
        bean.setImgResId(R.mipmap.add_administrator);
        bean.setTitle("添加管理员");
        bean.setDescribe("事情太多？力不从心？多来几个帮手吧");
        list.add(bean);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void initView() {
        listView = findViewById(R.id.data_lv);
        //API21以上才有效果----第二种方式，比较简便
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            listView.setNestedScrollingEnabled(true);
        }
        listView.setAdapter(mAdapter = new CommonAdapter<ManageMenuBean>(
                mActivity.getApplicationContext(), list, R.layout.item_manage_menu) {
            @Override
            public void convert(CommonViewHolder helper, ManageMenuBean item) {
                helper.setImageResource(R.id.manage_item_iv, item.getImgResId());
                helper.setText(R.id.manage_item_title_tv, item.getTitle());
                helper.setText(R.id.manage_item_describe_tv, item.getDescribe());
            }

        });
        checkManager();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                switch (position) {
                    case 0:
                        if (myUser.getLeague() == null) {
                            UIHelper.ToastMessage(mActivity, "请先申请加入社团");
                            return;
                        }
                        intent.setClass(mActivity, AlbumActivity.class);
                        startActivity(intent);
                        break;
                    case 1:
                        intent.setClass(mActivity, RecruitMainActivity.class);
                        intent.putExtra("isManager", isManager);
                        startActivity(intent);
                        break;

                    case 2:
                        if (myUser.getLeague() == null) {
                            UIHelper.ToastMessage(mActivity, "请先申请加入社团");
                            return;
                        }
                        intent.setClass(mActivity, LaunchNoticeActivity.class);
                        startActivity(intent);
                        break;
                    case 3:
                        if (myUser.getLeague() == null) {
                            UIHelper.ToastMessage(mActivity, "请先申请加入社团");
                            return;
                        }
                        intent.setClass(mActivity, LaunchEventActivity.class);
                        startActivity(intent);
                        break;
                    case 4:
                        intent.setClass(mActivity, UserListActivity.class);
                        startActivity(intent);
                        break;
                }
            }
        });
    }
}
